package pl.us.ij.plugins.segmentation.commands.threshold;

import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import pl.us.ij.plugins.segmentation.Trace;
import pl.us.ij.plugins.segmentation.infrastructure.Command;
import pl.us.ij.plugins.segmentation.infrastructure.exceptions.BreakCommandException;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-02-28
 * Time: 13:47:47
 * To change this template use File | Settings | File Templates.
 */
public class TwoLevelThresholdCommand extends Command<TwoLevelThresholdCommandParams> {
    @Override
    protected void processImage(FloatProcessor fp) throws BreakCommandException {

        float topThreshold = getParams().getTopThreshold();
        float bottomThreshold = getParams().getBottomThreshold();

        Trace.log("Top:" + topThreshold);
        Trace.log("Bottom:" + bottomThreshold);

        ImageProcessor ip = fp;

        float[] pixels = (float[]) ip.getPixels();

        for (int i = 0; i < pixels.length; i++) {
            float pixelValue = pixels[i];

            if (pixelValue < bottomThreshold || pixelValue > topThreshold) {
                pixels[i] = 0;
            } else {
                pixels[i] = 255;
            }
        }
    }
}
